/**
 * Created by PunkVv <punkv@qq.com> on 2016/6/17.
 */
$(function () {
    /**
     * 表单ajax-post提交
     */
    $(".form-ajax").on("submit", function (e) {
        e.preventDefault();
        var target, query, form;
        var nead_confirm = false;
        form = $(this);
        if (parsley(form)) {
            if (form.get(0) == undefined) {
                return false;
            } else if (form.get(0).nodeName == 'FORM') {
                if ($(this).hasClass('confirm')) {
                    if (!confirm('确认要执行该操作吗?')) {
                        return false;
                    }
                }
                target = form.get(0).action;
                query = form.serialize();
            } else if (form.get(0).nodeName == 'INPUT' || form.get(0).nodeName == 'SELECT' || form.get(0).nodeName == 'TEXTAREA') {
                form.each(function (k, v) {
                    if (v.type == 'checkbox' && v.checked == true) {
                        nead_confirm = true;
                    }
                })
                if (nead_confirm && $(this).hasClass('confirm')) {
                    if (!confirm('确认要执行该操作吗?')) {
                        return false;
                    }
                }
                query = form.serialize();
            } else {
                if ($(this).hasClass('confirm')) {
                    if (!confirm('确认要执行该操作吗?')) {
                        return false;
                    }
                }
                query = form.find('input,select,textarea').serialize();
            }
            var that = form.find(':submit');
            that.addClass('disabled').attr('autocomplete', 'off').prop('disabled', true);//防止表单重复提交
            $.post(target, query).success(function (data) {
                that.removeClass('disabled').prop('disabled', false);
                ajax_call_back(data);
            });
        }
    });
});